package com.loanapp.server.market.webapp.controller.bbs;

import com.github.pagehelper.PageInfo;
import com.loanapp.base.common.constants.AppConstant;
import com.loanapp.base.common.dto.ResponseDto;
import com.loanapp.base.common.exception.BizExceptionEnum;
import com.loanapp.base.common.exception.BusinessException;
import com.loanapp.base.common.utils.StringUtil;
import com.loanapp.base.controller.support.HttpKit;
import com.loanapp.base.controller.utils.PageUtil;
import com.loanapp.server.market.api.bbs.api.PostApi;
import com.loanapp.server.market.api.bbs.entity.Post;
import com.loanapp.server.market.api.bbs.query.PostQuery;
import com.loanapp.server.market.webapp.constant.LoginValidate;
import com.loanapp.server.user.api.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("post")
@Api(value = "帖子控制器", description = "帖子API")
public class PostController {

    @Autowired
    private PostApi postApi;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "列子列表（带评论数）", notes="",
            produces = "application/json; charset=utf-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页多少行（默认值5）",
                    dataType = "int", paramType = "query", defaultValue=""),
            @ApiImplicitParam(name = "pageNum", value = "当前页",
                    dataType = "int", paramType = "query", defaultValue=""),

    })
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto postList(PostQuery query) throws Exception {
        ResponseDto result= ResponseDto.build(BizExceptionEnum.SUCCESSFUL_OPERATION);
            query.setPageParams( PageUtil.init(HttpKit.getRequest()));
            PageInfo page= postApi.getPostListPage(query);
            if(page==null){
                 ResponseDto.build(BizExceptionEnum.SERVER_REQUEST_FAILED);
            }
            else {
                result.setData(page);
            }
            return result;
    }

    @ApiOperation(value = "帖子详情", notes="通过帖子id获取帖子详情,带3条评论",
            produces = "application/json; charset=utf-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "帖子id",
                    dataType = "int", paramType = "query", defaultValue=""),
    })
    @RequestMapping(value = "getPost",method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto getPostWithCommentByPid(Model model,@RequestParam(required = false) Long pid) throws Exception {
        ResponseDto result=ResponseDto.build(BizExceptionEnum.SUCCESSFUL_OPERATION);

            if(StringUtil.isEmpty(pid)){
                return ResponseDto.build(BizExceptionEnum.REQUEST_PARAMS_NULL);
            }
            Map map=   postApi.getPostWithCommentById(pid);
            result.setData(map);
        return  result;
    }

    @ApiOperation(value = "添加帖子", notes="只需填必填项",
            produces = "application/json; charset=utf-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "帖子标题",
                    dataType = "int", paramType = "query", defaultValue="",required = true),
            @ApiImplicitParam(name = "content", value = "帖子内容",
                    dataType = "int", paramType = "query", defaultValue="",required = true),
    })
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto InsertPost(Post post) throws Exception {
        ResponseDto result=ResponseDto.build(BizExceptionEnum.SUCCESSFUL_OPERATION);
        String sso_key= HttpKit.getRequest().getHeader("Authorization");
        if(StringUtil.isEmpty(sso_key)){
            throw  new BusinessException(BizExceptionEnum.NO_THIS_USER);
        }
        User user= (User) redisTemplate.opsForValue().get(sso_key);
        if(user==null){
            return ResponseDto.build(BizExceptionEnum.REQUEST_FAILED);
        }
            if(user!=null){
                post.setPostPid(user.getId());
                post.setPostPname(user.getPhone());
                post.setTopRank(0);
            }
            else {
                return ResponseDto.build(BizExceptionEnum.REQUEST_FAILED);
            }
            boolean canPost= postApi.CanPost(user.getId());
            if(canPost){
                post.setPostType(2);
                Long rs= postApi.Insert(post);
                if(rs==null) {
                    return ResponseDto.build(BizExceptionEnum.REQUEST_FAILED);
                }
            }
            else {
                return ResponseDto.build(AppConstant.Code.FAIL,"你已被禁言，请联系管理员");
            }
        return result;
    }
}
